package com.ruoyi.zjgl.psxz.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.zjgl.psxz.domain.Psxz;
import com.ruoyi.zjgl.psxz.domain.PsxzFj;
import com.ruoyi.zjgl.psxz.service.IPsxzService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 评审须知Controller
 *
 * @author huacai
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/zjgl/psxz")
public class PsxzController extends BaseController {
    @Resource
    private IPsxzService psxzService;

    /**
     * 查询评审须知列表
     */
    @PreAuthorize("@ss.hasPermi('zjgl:psxz:list')")
    @GetMapping("/list")
    public TableDataInfo list(Psxz psxz) {
        startPage();
        List<Psxz> list = psxzService.selectPsxzList(psxz);
        return getDataTable(list);
    }

    /**
     * 导出评审须知列表
     */
    @PreAuthorize("@ss.hasPermi('zjgl:psxz:export')")
    @Log(title = "评审须知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Psxz psxz) {
        List<Psxz> list = psxzService.selectPsxzList(psxz);
        ExcelUtil<Psxz> util = new ExcelUtil<Psxz>(Psxz.class);
        util.exportExcel(response, list, "评审须知数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Psxz> util = new ExcelUtil<Psxz>(Psxz.class);
        util.importTemplateExcel(response, "评审须知数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "评审须知", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('zjgl:psxz:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Psxz> util = new ExcelUtil<Psxz>(Psxz.class);
        InputStream inputStream = file.getInputStream();
        List<Psxz> list = util.importExcel(inputStream);
        inputStream.close();
        int count = psxzService.batchInsertPsxz(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取评审须知详细信息
     */
    @PreAuthorize("@ss.hasPermi('zjgl:psxz:query')")
    @GetMapping(value = "/{psxzId}")
    public AjaxResult getInfo(@PathVariable("psxzId") String psxzId) {
        return success(psxzService.selectPsxzByPsxzId(psxzId));
    }

    /**
     * 新增评审须知
     */
    @PreAuthorize("@ss.hasPermi('zjgl:psxz:add')")
    @Log(title = "评审须知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Psxz psxz) {
        psxz.setUserId(Math.toIntExact(getUserId())); //用户ID
        psxz.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        psxz.setCreateBy(getUsername()); //创建人
        psxz.setIdWhere("存在");
        return toAjax(psxzService.insertPsxz(psxz));
    }

    /**
     * 修改评审须知
     */
    @PreAuthorize("@ss.hasPermi('zjgl:psxz:edit')")
    @Log(title = "评审须知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Psxz psxz) {
        return toAjax(psxzService.updatePsxz(psxz));
    }

    /**
     * 删除评审须知
     */
    @PreAuthorize("@ss.hasPermi('zjgl:psxz:remove')")
    @Log(title = "评审须知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{psxzIds}")
    public AjaxResult remove(@PathVariable String[] psxzIds) {
        return toAjax(psxzService.deletePsxzByPsxzIds(psxzIds));
    }

    /**
     * 获得评审须知主键ID (UUID)
     */
    @GetMapping("/getPsXzId")
    public AjaxResult getPsXzId() {
        String getPsXzId = IdUtils.fastSimpleUUID();
        return success(getPsXzId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertPsxzFjByPsxzId(@RequestBody PsxzFj psxzFj) {
        return toAjax(psxzService.insertPsxzFjByPsxzId(psxzFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectPsxzFjList(String sId) {
        PsxzFj psxzFj = new PsxzFj();
        psxzFj.setPsxzId(sId);
        return AjaxResult.success(psxzService.selectPsxzFjList(psxzFj));
    }

    /**
     * 删除评审须知附件
     */
    @Log(title = "评审须知附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deletePsxzFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(psxzService.deletePsxzFjByFjIds(fjIds));
    }

}
